<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
use App\User;
class ArticleController extends Controller
{
    //
    public function index(){
        $article=Article::paginate(10);
        for($i=0;$i<10;$i++){
            $article[$i]['username']=User::find($article[$i]['user_id'])['name'];
        }
        return $article;
    }
    public function edit($id){
        $article=Article::find($id);
        $article['username']=User::find($article['user_id'])['name'];
        return $article;
    }
    public function destroy($id){
        if(Article::find($id)->delete()){
            echo '删除成功';
        }       
    }
    public function update(Request $req){
        $user=Article::find($req->id);
        $user->title=$req->title;
        $user->body=$req->body;
        //return '更新成功';
        if($user->save()){
            echo '数据更新成功！';
        }
    }
     public function batchdestroy(Request $req){
        $ids=explode(',',$req->ids);
        foreach ($ids as $id){
            Article::find($id)->delete();
        }
        echo '删除成功';
    }
}
